home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / snz128s / src / locking.c < prev    next >
C/C++ Source or Header  |  1994-05-23  |  3KB  |  100 lines

  1. /*
  2.     SNEWS 2.0
  3.  
  4.     locking - a simple lock file manager
  5.  
  6.  
  7.     Copyright (C) 1991    Malcolm S. Muir, Sunderland, UK.
  8.                         malcolm@muir.demon.co.uk
  9.  
  10.     This program is free software; you can redistribute it and/or modify
  11.     it under the terms of the GNU General Public License, version 1, as
  12.     published by the Free Software Foundation.
  13.  
  14.     This program is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.     GNU General Public License for more details.
  18.  
  19.     See the file COPYING, which contains a copy of the GNU General
  20.     Public License.    
  21.  */
  22.  
  23. /*---------------------------- Source Control ------------------------------*/
  24.  
  25. /*
  26.  * $Id: LOCKING.C,v 1.2 1994/02/05 18:48:42 gbj Exp user $
  27.  */
  28.  
  29. /****************************************************************************
  30. *   01 Jun 93   1.1    MSM  Header added                                    *
  31. *                           Snews 2.0                                       *
  32. *   09 Jul 93   1.2    MSM  Correct error in lock file creation with short  *
  33. *                           names.                                          *
  34. ****************************************************************************/
  35.  
  36. #include <stdio.h>
  37. #include <io.h>
  38. #include <fcntl.h>
  39. #include <string.h>
  40. #include "locking.h"
  41.  
  42. #ifdef ATARI
  43. #    include "fileops.h"
  44. #endif
  45.  
  46. /*
  47.  * create a lockfile
  48.  */
  49.  
  50. int             mlock(char *dir, char *id, char *name)
  51. {
  52.     int             i;
  53.     char            lockname[65], tempname[65];
  54.     int             fd;
  55.  
  56.     strcpy(tempname, id);
  57.     if (strlen(tempname) > 8)               /* truncate long filenames */
  58.         tempname[8] = '\0';
  59.     if (tempname[7] == '\\')
  60.         tempname[7] = '\0';
  61.     for (i = 0; i < 7; i++)
  62.         if (tempname[i] == '.') {
  63.             tempname[i] = '\0';
  64.             break;
  65.         }
  66.  
  67.     /* Try to create the lock file in an atomic operation */
  68.     sprintf(lockname, "%s%s.lck", dir, tempname);
  69.     if ((fd = open(lockname, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
  70.         return -1;
  71.     write(fd, name, strlen(name));
  72.     close(fd);
  73.     return 0;
  74. }
  75.  
  76. /*
  77.  * remove a lockfile
  78.  */
  79.  
  80. int             rmlock(char *dir, char *id, char *name)
  81. {
  82.     int             i;
  83.     char            lockname[65], tempname[65];
  84.  
  85.     name = name;                          /* to remove error message */
  86.  
  87.     strcpy(tempname, id);
  88.     if (strlen(tempname) > 8)              /* truncate long filenames */
  89.         tempname[8] = '\0';
  90.     if (tempname[7] == '/')
  91.         tempname[7] = '\0';
  92.     for (i = 0; i < 7; i++)
  93.         if (tempname[i] == '.') {
  94.             tempname[i] = '\0';
  95.             break;
  96.         }
  97.     sprintf(lockname, "%s/%s.lck", dir, tempname);
  98.     return (unlink(lockname));
  99. }
  100.